#include "Echart.h"
#include "echart_private.h"

#include <stdio.h>
#include <string.h>
/*============================================================================*
 *                                  Local                                     *
 *============================================================================*/
static Eina_Bool _bar_chart_parse(Echart *ec, char *str)
{
	Echart_Bar_Type chbt;

	chbt = echart_bar_type_get(ec);
	switch (chbt)
	{
		case ECHART_BAR_HORIZONTAL_STACKED:
		strcat(str, "&cht=bhs");
		break;

		case ECHART_BAR_VERTICAL_STACKED:
		strcat(str, "&cht=bvs");
		break;

		case ECHART_BAR_VERTICAL_OVERLAPPED:
		strcat(str, "&cht=bvo");
		break;

		case ECHART_BAR_HORIZONTAL_GROUPED:
		strcat(str, "&cht=bhg");
		break;

		case ECHART_BAR_VERTICAL_GROUPED:
		strcat(str, "&cht=bvg");
		break;
	}
	return EINA_TRUE;
}

static Eina_Bool _chart_parse(Echart *ec, char *str)
{
	Echart_Type cht;
	unsigned int width;
	unsigned int height;

	echart_size_get(ec, &width, &height);
	sprintf(str, "%schs=%dx%d", str, width, height);

	cht = echart_type_get(ec);
	switch (cht)
	{
		case ECHART_TYPE_BAR:
		_bar_chart_parse(ec, str);
		break;
	}
	return EINA_TRUE;
}


static Eina_Bool _dataset_data_parse(Echart *ec, Echart_Dataset *ds, char *str)
{
	Echart_Type cht;
	Echart_Dataset *next;
	double *data;
	int data_count;
	int i;

	/* add the data */
	data_count = echart_dataset_values_get(ds, &data);
	if (!data_count) return EINA_FALSE;

	sprintf(str, "%s%f", str, data[0]);
	for (i = 1; i < data_count; i++)
		sprintf(str, "%s,%f", str, data[i]);
	/* chat type specific paarameters */
	cht = echart_type_get(ec);
	free(data);

	return EINA_TRUE;
}

static Eina_Bool _dataset_parse(Echart *ec, char *str)
{
	Echart_Dataset *ds;

	ds = echart_dataset_get(ec);
	if (!ds) return EINA_FALSE;

	/* sets the data */
	/* FIXME check the correct format */
	strcat(str, "&chd=t:");
	while (ds)
	{
		_dataset_data_parse(ec, ds, str);
		ds = echart_dataset_next(ds);
		/* add the separator */
		if (ds) strcat(str, "|");
	}
	/* sets the color */
	ds = echart_dataset_get(ec);
	strcat(str, "&chco=");
	while (ds)
	{
		Echart_Color color;

		color = echart_dataset_color_get(ds);
		sprintf(str, "%s%06X", str, (color & 0x00ffffff));
		ds = echart_dataset_next(ds);
		/* add the separator */
		if (ds) strcat(str, "|");
	}
	/* sets the labels */
	ds = echart_dataset_get(ec);
	strcat(str, "&chdl=");
	while (ds)
	{
		char *label;

		label = echart_dataset_label_get(ds);
		if (label) strcat(str, label);
		ds = echart_dataset_next(ds);
		/* add the separator */
		if (ds) strcat(str, "|");
	}
}

static void _generate(Echart *ec, void *data)
{
	Echart_Dataset *ds;
	char **uri = (char **)data;
	char tmp[2048];

	strcpy(tmp, "http://chart.apis.google.com/chart?");
	_chart_parse(ec, tmp);
	_dataset_parse(ec, tmp);
	*uri = strdup(tmp);
}

Echart_Backend _backend = {
	_generate,
};
/*============================================================================*
 *                                 Global                                     *
 *============================================================================*/
/*============================================================================*
 *                                   API                                      *
 *============================================================================*/
EAPI Echart_Backend * echart_gchart_get(void)
{
	return &_backend;
}
